#coding=utf-8
import os
import sys
import chardet
import cx_Oracle
from datetime import datetime
from com.jumang.content.BaseCheck import BaseCheck

class DbCheck(BaseCheck):
    type='oracle app'
    def __init__(self,fullString,event):
        BaseCheck.__init__(self,fullString,event)
        elements=fullString.split('||')
        if len(elements)==7:
          self.server=elements[3].strip()
          self.user=elements[4].strip()
          self.pwd=elements[5].strip()
          self.sql=elements[6].strip()
          self.errorNo=0
          self.errorInfo=''
        else:
          print 'Error: '+fullString
        
    def check(self):
      self.errorNo=0
      self.errorInfo=''
      c=None
      db=None
      #print self.errorNo
      try:
        db = cx_Oracle.connect(self.user, self.pwd , self.server,threaded=True)
        c = db.cursor()
        c.execute(self.sql)
        myDatas=c.fetchall()
        for myData in myDatas:
          #print myData[0]
          if myData[0]<0:
            self.errorNo=-555
            self.errorInfo=myData[1]+' is less than Zero'
            break;
           
        c.close()
        db.close()
        
      except cx_Oracle.Error, exc:
        error, = exc.args
        self.errorNo=error.code
        self.errorInfo=error.message
        if c!=None:
          try:
            c.close()
          except:
            pass
        if db:
          db.close()
      except:
        self.errorNo=-666
        self.errorInfo=sys.exc_info()[0]
        if c:
          try:
            c.close()
          except:
            pass
        if db:
          db.close()
      #print self.errorNo
      return (self.errorNo,self.errorInfo)
      